import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.Label;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.border.Border;
import javax.swing.border.LineBorder;

import java.sql.*;

public class Server {
	static ArrayList<Integer> section = new ArrayList<Integer>();
	static String[] menuTypes;
	static int menuLoc = 0;
	static int EmpID;

	public static void server(int empID) {
		GUI.window.setTitle("Server Screen");
		EmpID = empID;
		GUI.bottomPanel = GUI.exitOnly();
		// state="serverMain"; //Will be changed
		serverMain();

		GUI.windowDone();

	}

	static void serverMain() {
		// Load in the menu items here
		Connection c = null;
		ResultSet menuResults;
		try {
			c = SqliteHelper.getConn();
			Statement menuSelectStatement = c.createStatement();
			String menuSelectQuery = "SELECT * FROM Menu ORDER BY Specific_Cat, Item_Name;";
			ArrayList<String> menuArrayList = new ArrayList();
			menuResults = menuSelectStatement.executeQuery(menuSelectQuery);
			while (menuResults.next()) {
				menuArrayList.add(menuResults.getString("Item_Name"));
			}
			menuTypes = new String[menuArrayList.size()];
			for (int i = 0; i < menuTypes.length; i++) {
				menuTypes[i] = menuArrayList.get(i);
			}
		} catch (Exception ex) {
			System.err.println("Database Call Failure in serverMain.");
			System.err
					.println(ex.getClass().getName() + ": " + ex.getMessage());
		}
		// End of database menu load

		GUI.resetPanels();
		GUI.leftMenu.add(GUI.setSectionButton(EmpID));
		GUI.leftMenu.add(GUI.takeoutButton(40, EmpID));
		GUI.leftMenu.add(GUI.newButton("", 40));

		for (int i = 1; i < 5; i++) {
			for (int j = 1; j < 5; j++) {
				Integer table = j * 10 + i;
				if (contains(table, section))
					GUI.centerPanel.add(newTable(table, 75, GUI.blue,
							Color.white));
				else
					GUI.centerPanel.add(newTable(table, 75, GUI.yellow,
							Color.black));
			}
		}
		GUI.leftMenu.setFont(new Font("Arial", Font.PLAIN, 40));
		GUI.bottomPanel.add(GUI.exitOnly());
	}

	static boolean contains(Integer table, ArrayList<Integer> section) {
		if (section == null)
			return false;
		for (int i = 0; i < section.size(); i++) {
			if (table == section.get(i))
				return true;
		}
		return false;
	}

	static boolean contains(Integer table, Integer[] section) {
		for (int i = 0; i < section.length; i++) {
			if (table == section[i])
				return true;
		}
		return false;
	}

	public static JButton newTable(Integer tableNum, int size, Color color,
			Color text) {
		JButton button = GUI.newButton(Integer.toString(tableNum), 40);
		button.setBackground(color);
		button.setForeground(text);
		Border thickBorder = new LineBorder(GUI.teal, size);
		button.setBorder(thickBorder);
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (color == GUI.yellow) {
					boolean myTable = PopUp.takeTable();
					if (!myTable) {
						return;
					} else {
						section.add(tableNum);
						button.setBackground(GUI.blue);
						button.setForeground(Color.white);
					}
				}
				int customers = PopUp.getNumCustomers();// This should really be
														// called from the
														// ActionListener
				if (customers > -1) {
					serverAtTable(tableNum, menuTypes, EmpID, customers);
					// serverAtTable(tableNum, menuTypes, EmpID); Original code
				} else {
					GUI.reset();
				}
			}

		});
		return button;
	}

	public static JButton newSectionTable(Integer tableNum, int size, Color text) {
		JButton button = GUI.newButton(Integer.toString(tableNum), 40);
		if (contains(tableNum, section))
			button.setBackground(GUI.blue);
		else
			button.setBackground(GUI.yellow);
		button.setForeground(text);
		Border thickBorder = new LineBorder(GUI.teal, size);
		button.setBorder(thickBorder);
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (button.getBackground() == GUI.yellow) {
					button.setBackground(GUI.blue);
					section.add(tableNum);
				} else {
					button.setBackground(GUI.yellow);
					section.remove(tableNum);
				}
			}

		});
		return button;
	}

	static JButton nextButton(int size, String[] array) { // TODO Fix this
		JButton temp = new JButton("Next");
		temp.setFont(new Font("Arial", Font.PLAIN, size));
		if (menuLoc + 8 < array.length)
			menuLoc += 8;
		else
			menuLoc = 0;
		temp.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				GUI.resetPanels();
				// serverAtTable(menuLoc, array, EmpID); Original code -
				// disabled
			}
		});
		return temp;
	}

	static void serverAtTable(Integer tableNum, String[] menuOptions, int id,
			int customers) {
		GUI.resetPanels();
		// ArrayList<String> tempArray=new ArrayList<String>();
		// if(menuOptions.length-menuLoc<9){
		// for(int i=0;i<menuOptions.length;i++){
		// GUI.leftMenu.add(GUI.menuButton(menuOptions[i],40));
		// }
		// for(int i=0;i<(9-menuOptions.length);i++){
		// GUI.leftMenu.add(GUI.emptyButton());
		// }
		// }
		// if(menuOptions.length-menuLoc>9){
		// for(int i=menuLoc;i<menuLoc+8;i++){
		// GUI.leftMenu.add(GUI.menuButton(menuOptions[i],40));
		// }
		// GUI.leftMenu.add(nextButton(40, menuOptions));
		// }
		if (tableNum >= 100)
			GUI.window.setTitle("Takeout Order #" + tableNum);
		else
			GUI.window.setTitle("Table #" + tableNum);
		GUI.centerPanel.setLayout(new BorderLayout());
		GUI.centerPanel.add(new TicketGrid(customers)); // //// REPLACE LINE 109
														// WITH THIS GRID /////
		// GUI.centerPanel.add(new Label("Table "+tableNum.toString()));
		// GUI.leftMenu.setFont(new Font("Arial", Font.PLAIN, 40));

		GUI.bottomPanel.add(GUI.tableBottomPanel(id));
		Server.leftMenu(menuOptions, 0);
	}

	private static void leftMenu(String[] menuOptions, int menuAt) {
		JPanel leftMenu = new JPanel();
		leftMenu.setLayout(new GridLayout(3, 3));
		leftMenu.setSize(500, 500);
		leftMenu.setFont(new Font("Arial", Font.PLAIN, 40));
		GUI.leftPanel.removeAll();
		JButton nextButton = GUI.newButton("Next", 40);
		for (int i = menuAt; i < menuAt + 8; i++) {
			if (i < menuOptions.length)
				leftMenu.add(GUI.menuButton(menuOptions[i], 40));
			else
				leftMenu.add(GUI.emptyButton());
		}
		if (menuOptions.length > 8)
			leftMenu.add(nextButton);
		else
			leftMenu.add(GUI.emptyButton());
		nextButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (menuAt + 8 < menuOptions.length)
					leftMenu(menuOptions, menuAt + 8);
				else
					leftMenu(menuOptions, 0);
				// serverAtTable(menuLoc, array, EmpID); Original code -
				// disabled
			}
		});
		GUI.leftMenu.removeAll();
		GUI.leftMenu = leftMenu;
		GUI.windowDone();
	}

	public static void setSections(int empID2) {
		GUI.resetPanels();
		JButton sections = GUI.setSectionButton(EmpID);
		sections.setBackground(Color.DARK_GRAY);
		GUI.leftMenu.add(sections);
		GUI.leftMenu.add(GUI.takeoutButton(40, EmpID));
		GUI.leftMenu.add(GUI.newButton("View Active Tables As List", 40));

		for (int i = 1; i < 5; i++) {
			for (int j = 1; j < 5; j++) {
				Integer table = j * 10 + i;
				GUI.centerPanel.add(newSectionTable(table, 75, Color.white));
			}
		}
		GUI.leftMenu.setFont(new Font("Arial", Font.PLAIN, 40));
		GUI.bottomPanel.add(GUI.exitOnly());
	}

}
